package com.papper.mng.dialect.tag;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.papper.common.config.PapperStorage;
import com.papper.common.enums.utils.EnumsUtil;
import com.papper.common.utils.tool.StringTool;
import com.papper.mng.dialect.PapperDialect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.thymeleaf.context.ITemplateContext;
import org.thymeleaf.model.IModel;
import org.thymeleaf.model.IModelFactory;
import org.thymeleaf.model.IProcessableElementTag;
import org.thymeleaf.processor.element.AbstractElementTagProcessor;
import org.thymeleaf.processor.element.IElementTagStructureHandler;
import org.thymeleaf.templatemode.TemplateMode;

import java.util.List;

/**
 * @author:LaJiao
 * @date 2022年04月02日
 * @time 08时06
 * @Description:
 */
public class PaEnumSelectTag extends AbstractElementTagProcessor {
    private static final Logger logger   = LoggerFactory.getLogger(PaEnumSelectTag.class);
    // 标签名
    private static final String TAG_NAME = "enumSelect";

    public PaEnumSelectTag(String dialectPrefix) {
        super(TemplateMode.HTML, dialectPrefix, TAG_NAME, true, null, false, PapperDialect.PRECEDENCE);
    }

    @Override
    protected void doProcess(ITemplateContext iTemplateContext, IProcessableElementTag iProcessableElementTag, IElementTagStructureHandler iElementTagStructureHandler) {
        //  获取前端页面传递的属性
        String enumName = iProcessableElementTag.getAttributeValue("enum");
        String name     = iProcessableElementTag.getAttributeValue("name");
        String code     = iProcessableElementTag.getAttributeValue("code");
        String desc     = iProcessableElementTag.getAttributeValue("desc");
        String empty    = iProcessableElementTag.getAttributeValue("empty");
        String scope    = iProcessableElementTag.getAttributeValue("scope");

        // Enum获取
        StringBuilder enumPath = new StringBuilder();
        enumPath.append(PapperStorage.Config("SystemConfig", "base.enum.package", "com.papper.common.enums."));
        enumPath.append(enumName);
        enumPath.append("Enum");
        Class     enumClass = null;
        JSONArray enumArray = new JSONArray();
        try {
            enumClass = Class.forName(enumPath.toString());
            enumArray = EnumsUtil.enumToMap(enumClass);
        } catch (ClassNotFoundException e) {
            logger.error("属性对象不存在:[{}],[{}]", enumName, enumPath);
        }
        StringBuffer stingBuffer = new StringBuffer();
        stingBuffer.append("<select name='").append(name).append("' data-placeholder='请选择").append(desc).append("...' class='chosen-select' style='width:100%;'>");
        if ("1".equalsIgnoreCase(empty)) {
            stingBuffer.append("<option value=''>--请选择").append(desc).append("--</option>");
        }
        List<String> scopeList = StringTool.stringToListString(scope, ",");
        for (int i = 0; i < enumArray.size(); i++) {
            JSONObject jsonObject = enumArray.getJSONObject(i);
            String     jCode      = jsonObject.getString("code");
            String     jValue     = jsonObject.getString("value");
            if (scopeList != null && !scopeList.isEmpty() && !scopeList.contains(jCode)) {
                continue;
            }
            if (jCode.equalsIgnoreCase(code)) {
                stingBuffer.append("<option value='").append(jCode).append("' hassubinfo='true' selected>").append(jValue).append("</option>");
            } else {
                stingBuffer.append("<option value='").append(jCode).append("' hassubinfo='true'>").append(jValue).append("</option>");
            }
        }
        stingBuffer.append("</select>");
        // 创建标签
        IModelFactory modelFactory = iTemplateContext.getModelFactory();
        IModel        model        = modelFactory.createModel();
        model.add(modelFactory.createText(stingBuffer.toString()));
        iElementTagStructureHandler.replaceWith(model, false);
    }
}
